<?php

namespace App\Admin\Controllers\OwnerItem;

use App\Admin\Repositories\OwnerItem;
use App\Models\Building;
use App\Models\House;
use App\Models\Owner;
use App\Models\Room;
use Carbon\Carbon;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

abstract class CommonOwnerItemController extends AdminController
{

    abstract protected function map(): array;

    abstract protected function customGrid(Grid $grid);

    protected function grid()
    {
        return Grid::make(new OwnerItem(['owner', 'owner.ownerable', 'owner.user']), function (Grid $grid) {
            $grid->model()->where($this->map())->orderBy('count', 'asc');
            $grid->id->sortable();
            $grid->column('owner.user', '业主姓名')->display(function ($value) {
                return $value['name'];
            });
            $grid->column('owner.ownerable', '关联的房间（楼栋或房产）')->display(function ($value) {
                if (isset($value['building_id'])) {  // 房间详细名称显示
                    $building = Building::query()->with('house')->find($value['building_id']);
                    return $building->house->name . " - " . $building->name. " - " . $value['name'];
                }
                if (isset($value['house_id'])) {
                    $house = House::query()->find($value['house_id']);
                    return $house->name . " - ". $value['name'];
                }
                return $value['name'];
            });
            $grid->count('第几期')->display(function ($value) {
                return "第{$value}期租金";
            });
            $grid->pay_at('支付时间')->display(function ($value) {
                return $value ? $this->total_amount : "未支付";
            });
            $grid->rent('本期租金')->display(function ($value) {
                return $value;
            });
            $grid->status('支付状态')->display(function ($value) {
                return \App\Models\OwnerItem::$statusMap[$value];
            });
            $grid->total_amount('总金额')->display(function ($value) {
                return $value ? "本期总租金：" . $value . "元": "未填写";
            });
            $this->customGrid($grid);
            $grid->started_at('租期开始时间')->sortable();
            $grid->ended_at('租期结束时间');

            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableBatchDelete();

            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
                $filter->panel();
                // 注意切换为panel布局方式时需要重新调整表单字段的宽度
                $filter->equal('id', '流水编号')->width(3);
                $filter->between('started_at', '最早交租时间')->width(3)->date();
                $filter->between('ended_at', '最晚交租时间')->width(3)->date();
                $filter->between('pay_at', '支付时间')->width(3)->datetime();
                $filter->equal('count', '第几期交租')->width(3);
                $filter->between('rent', '租金范围')->width(3);
            });
        });
    }

    abstract protected function customDetail($show);

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new OwnerItem(), function (Show $show) {
            $show->id;
            $show->owner_id;
            $show->count;
            $show->pay_at;
            $show->rent;
            $show->other_amount;
            $show->status;
            $show->total_amount;
            $show->started_at;
            $show->ended_at;
            $show->created_at;
            $show->updated_at;
        });
    }
}
